package dc.medicine.com.okhttplib.request;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by hp on 2016/7/15.
 */
public class PostRequest extends BaseRequest<PostRequest> {


    public static final MediaType MEDIA_TYPE_PLAIN = MediaType.parse("text/plain;charset=utf-8");
    public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/x-www-form-urlencoded;charset=utf-8");
    public static final MediaType MEDIA_TYPE_STREAM = MediaType.parse("application/octet-stream");

    private MediaType mediaType; //上传的MIME类型
    private String string;       //上传的文本内容
    private String json;         //上传的Json
    private byte[] bs;           //上传的字节数据

    public PostRequest(String url) {
        super(url);
    }

    @Override
    protected RequestBody generateRequestBody() {
        if(string != null && mediaType !=null)return RequestBody.create(mediaType,string);
        if(json !=null && mediaType !=null)return RequestBody.create(mediaType,json);
        if(bs !=null && mediaType !=null)return RequestBody.create(mediaType,bs);
        return generateMultipartRequestBody(MEDIA_TYPE_JSON);
    }

    @Override
    protected Request generateRequest(RequestBody requestBody) {
        //Request.Builder requestbuilder=new Request.Builder();
//        try {
//            headers.put("Content-Length",String.valueOf(requestBody.contentLength()));
//        }catch (Exception e){
//            e.printStackTrace();
//        }
//        appendHeaders(requestbuilder);
        return new Request.Builder().post(requestBody).url(url).tag(tag).build();
    }



}
